import moment from 'moment';

function isRests(year = new Date().getFullYear()) {
  return new Promise((resolve, reject) => {
    fetch(`https://date.nager.at/api/v3/LongWeekend/${year}/CN`)
      .then((res) => res.json())
      .then((data) => {
        resolve(data);
      })
      .catch((error) => {
        reject(error);
      });
  });
}

function isTeams() {
  const year = new Date().getFullYear();
  return new Promise((resolve, reject) => {
    fetch('https://api.comm.miui.com/holiday/holiday.jsp')
      .then((res) => {
        console.log(res);
        return res.json();
      })
      .then((data) => {
        const teams = data.find((item) => item.year === year);

        resolve(teams);
      })
      .catch((error) => {
        reject(error);
      });
  });
}

export async function getYearAllDaysWork(
  param,
  year = new Date().getFullYear()
) {
  const { workday, freeday } = param;
  let days = [];
  for (let month = 0; month < 12; month++) {
    let daysInMonth = new Date(year, month + 1, 0).getDate(); // 获取月份的天数
    for (let day = 1; day <= daysInMonth; day++) {
      days.push(moment(new Date(year, month, day)).format('YYYY-MM-DD'));
    }
  }
  const workObj = {};
  days.forEach((day, i) => {
    const isWork = workday.find((n) => n === i + 1);
    const isRreeday = freeday.find((n) => n === i + 1);
    workObj[day] = { date: day, num: i + 1, isWork: isWork ? true : false , isRreeday : isRreeday ? true : false};
  });
  return workObj;
}
